/*
 * Copyright 2017, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ctyon.watchmanager.ui.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.ctyon.watchmanager.R;
import com.ctyon.watchmanager.databinding.ItemWatchRecyclerBinding;
import com.ctyon.watchmanager.listener.ItemClickListener;
import com.ctyon.watchmanager.model.net.entity.Watch;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

public class WatchListAdapter extends RecyclerView.Adapter<WatchListAdapter.WatchViewHolder> {

    List<Watch> mWatchList = new ArrayList<>();

    @Nullable
    private final ItemClickListener chatsItemClickListener;

    public WatchListAdapter(@Nullable ItemClickListener itemClickListener) {
        chatsItemClickListener = itemClickListener;
        setHasStableIds(true);
    }

    public void setWatchList(final List<Watch> watchList) {
        mWatchList.clear();
        mWatchList.addAll(watchList);
        notifyDataSetChanged();
    }

    @Override
    public WatchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.d("Lee_WatchListAdapter", "onCreateViewHolder");
        ItemWatchRecyclerBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_watch_recycler, parent, false);
        binding.setListener(chatsItemClickListener);
        return new WatchViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(WatchViewHolder holder, int position) {
        Log.d("Lee_WatchListAdapter", "onBindViewHolder");
        holder.binding.setWatch(mWatchList.get(position));
        holder.binding.setIndex(position);
        holder.binding.executePendingBindings();
    }

    @Override
    public int getItemCount() {
        Log.d("Lee_WatchListAdapter", "getItemCount");
        return mWatchList == null ? 0 : mWatchList.size();
    }

    @Override
    public long getItemId(int position) {
        Log.d("Lee_WatchListAdapter", "getItemId position = " + position);
        return position;
    }

    static class WatchViewHolder extends RecyclerView.ViewHolder {

        final ItemWatchRecyclerBinding binding;

        public WatchViewHolder(ItemWatchRecyclerBinding binding) {
            super(binding.getRoot());
            Log.d("Lee_WatchListAdapter", "ChatsViewHolder");
            this.binding = binding;
        }
    }
}
